package com.glela.controller;

import com.alibaba.fastjson.JSONObject;
import com.glela.goods.service.AnswerQuestionService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/answerQuestion")
public class AnswerQuestionController extends BaseController{

    @Autowired
    private AnswerQuestionService answerQuestionService;

    @RequestMapping(value = "/add",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> add(@RequestBody JSONObject json){
        if (ParameterUtils.validParams(json.getString("userId"))){
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
        }
        Map<String,Object> map = new HashMap<>();
        if(answerQuestionService.getCountByUid(json.getInteger("userId"))>0){
            map.put("success",1);
            return new ResponseEntity<>(new ResponseResultModel<>(map),
                    HttpStatus.OK);
        }
        int count =answerQuestionService.addAnswerQuestion(json.getInteger("userId"));
        if (count>0){
            map.put("success",1);
        }else{
            map.put("success",0);
        }
        return new ResponseEntity<>(new ResponseResultModel<>(map),
                HttpStatus.OK);
    }
}
